<template>
	<div id="AccepterOfGener">
        <div v-if="isShowMsg">
            <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label>
                    <el-select v-model="value" filterable placeholder="请选择" :filter-method="changeSelect" @change="blurSelect">
                        <el-option v-for="item in options" :key="item.No" :label="item.Name" :value="item.No"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-popover placement="bottom" width="400" trigger="click">
                        <el-button type="primary" @click="determine">确定</el-button>
                        <el-button type="primary" @click="superior">上一级</el-button>
                        <el-tree
                            :data="data"
                            show-checkbox
                            :props="defaultProps"
                            @node-click="handleNodeClick"
                            @check-change="handleCheckChange"
                            ref="tree"
                        ></el-tree>
                        <el-button type="primary" slot="reference">选择人员</el-button>
                    </el-popover>
                    <el-button type="primary" @click="onSubmit">执行发送</el-button>
                    <!-- <el-button type="primary" @click="onSubmit">关闭</el-button> -->
                    <el-link :underline="false">查看帮助 ?</el-link>
                </el-form-item>
            </el-form>
            <el-table :data="tableData" style="width: 100%;height:200px;overflow:scroll;">
                <el-table-column prop="EmpName" label="姓名"></el-table-column>
                <el-table-column prop="DeptName" label="所在部门"></el-table-column>
                <el-table-column label="操作" width="150">
                    <template slot-scope="scope">
                        <el-button @click="Up(scope.row.MyPK)" type="text" size="small">上移</el-button>
                        <el-button @click="Down(scope.row.MyPK)" type="text" size="small">下移</el-button>
                        <el-button @click="open(scope.row.MyPK)" type="text" size="small">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div v-else>
            <p v-for="(item,index) in msg" :key="index" v-html="item"></p>
        </div>
	</div>
</template>

<script>
import { WebUser , Entity} from "../../api/Gener.js";
export default {
	name: "AccepterOfGener",

	data() {
		return {
			blogTitle: "",
			formInline: {
				user: "",
				region: ""
			},
			tableData: [],
			data: [],
			defaultProps: {
				children: "children",
				label: "Name"
			},
			params: {},
			FK_Dept: "", //部门级别
			ParentNo: "", //控制上一级
			No: "", // 控制下一级
			judgeDept: "productionTrre", //判断渲染上级还是下级
			childrenJSON: [],
            checkAll: [],
            options: [],
            value: '',
            isShowMsg:true,
            msg:[],
		};
	},

	beforeCreate() {},

	created() {
        this.isShowMsg = true;
		this.params = this.$store.getters.getData;
		console.log("this.params", this.params);
		if (this.params.FK_Dept) {
			this.FK_Dept = this.params.FK_Dept;
		} else {
			var webUser = new WebUser();
			this.FK_Dept = webUser.FK_Dept;
		}
		// this.FK_Dept = this.params.FK_Dept ? this.params.FK_Dept :;
		this.loadData();
		this.loadTable();
	},

	methods: {
		// 获取数据
		loadData() {
			console.log("this.FK_Dept---", this.FK_Dept);
			var handler = new this.HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
			handler.AddPara("FK_Dept", this.FK_Dept);
			var data = handler.DoMethodReturnString("SelectEmps_Init");
			if (data.indexOf("err@") == 0) {
				alert(data);
				return;
			}

			this[this.judgeDept](JSON.parse(data));
		},

		// 获取表格数据
		loadTable() {
			console.log("获取表格数据", this.params);
			var handler = new this.HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
			handler.AddJson(this.params);
			var data = handler.DoMethodReturnString("AccepterOfGener_Init");
			// alert(data);
			if (data.indexOf("err@") == 0) {
				alert(data);
				window.parent.close();
				return;
            }
			console.log("表格数据data----", data);
            this.tableData = JSON.parse(data);
		},

		//生产部门树
		productionTrre(data) {
			this.data = [];
			console.log(data);
			data.Depts.forEach(item => {
				item.children = [];
				data.Emps.forEach(items => {
					if (item.No == items.FK_Dept) {
						item.children.push(items);
					}
				});
				// console.log('this.FK_Dept',this.FK_Dept)
				// console.log('item.ParentNo',item.No)
				if (this.FK_Dept == item.No) {
					this.data.push(item);
					this.ParentNo = item.ParentNo;
				} else {
					this.data[0].children.push(item);
				}
			});
			console.log("this.data", this.data);
		},

		//下级树渲染
		lowerLevel(data) {
			console.log("下级树", data);
			data.Emps.forEach(item => {
				this.childrenJSON.children.push(item);
			});
		},

		//上级
		superior() {
			this.judgeDept = "productionTrre";
			if (this.ParentNo == 0) {
				this.$message({
					message: "已经是第一级机构了",
					type: "warning"
				});
				return;
			}
			this.FK_Dept = this.ParentNo;
			this.loadData();
		},

		// tree候选
		handleCheckChange(data, checked, indeterminate) {
			console.log(data, checked, indeterminate);
		},

		//tree 确定
		determine() {
			let res = this.$refs.tree.getCheckedNodes();
			this.checkAll = [];
			res.forEach(item => {
				if (item.FK_Dept) {
					this.checkAll.push(item.No);
				}
            });
            console.log(this.checkAll.join(","));
            this.addEmpsExit(this.checkAll.join(","));
        },
        
        addEmpsExit(emps){
            console.log('执行添加人员1',this.checkAll.join(","));
            console.log('执行添加人员2',this.params);
            var handler = new this.HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            handler.AddJson(this.params); //参数ToNode, WorkID,FK_Node,FK_Flow
            handler.AddPara("AddEmps",emps ); //要增加的人员，多个可以用逗号分开.
            var data = handler.DoMethodReturnString("AccepterOfGener_AddEmps");
            console.log("添加人员",data);
            if (data.indexOf('err@') == 0) {
                alert(data);
                // window.location.href = window.location.href;
                return;
            }
            if (data.indexOf('info@') == 0) {
                // $(".Msg").show();
                // $(".Msg").html(data);
                return;
            }

            this.loadTable();
           
          
        },

		onSubmit() {
            console.log(this.data);
            var httphandler = new this.HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            httphandler.AddJson(this.params); //FK_Flow,FK_Node,WorkID,FID,ToNode 等参数.
            // $("#Btn_Send").attr("disabled", "disabled"); //把发送按钮设置不可用.
            // $("#Btn_Send").val("正在发送请稍候...");
            // $("#Btn_Close").attr("disabled", "disabled"); //关闭按钮设置不可以用.
            // $("#Btn_SelectEmps").attr("disabled", "disabled"); //选择人按钮不可以用.
            var data = httphandler.DoMethodReturnString("AccepterOfGener_Send");
             if (data.indexOf('err@') == 0) {  //如果发送失败.
                alert(data);
                // $(".Msg").show();
                // $(".Msg").html(data);
                // $(".Msg").css("color", "red");

                // $("#Btn_Send").attr("disabled", false);
                // $("#Btn_Send").val("执行发送");
                // $("#Btn_Close").attr("disabled", false);
                // $("#Btn_SelectEmps").attr("disabled", false);
                return;
            }
            console.log(data);
            this.msg = data.split("<br>")
            console.log(this.msg);
            // this.$Bus.$emit("sendSuccess",data);
            this.isShowMsg = false;
		},
		handleNodeClick(data) {
			console.log(data);
			if (!Number(data.No)) {
				this.$message({
					message: "请选择其他部门",
					type: "warning"
				});
				return;
			}
			if (this.data[0].No != data.No) {
				this.childrenJSON = data;
				this.judgeDept = "lowerLevel";
				console.log(data);
				this.FK_Dept = data.No;
				this.loadData();
			}
        },

        // 上移动
        Up(mypk) {
            console.log("mypk---",mypk)
            var en = new Entity("BP.WF.Template.SelectAccper", mypk);
            en.DoMethodReturnString("DoUp");
            setTimeout(()=>{
                this.loadTable();
            },100)
        },

        // 下移动
        Down(mypk) {
            var en = new Entity("BP.WF.Template.SelectAccper", mypk);
            en.DoMethodReturnString("DoDown");
            setTimeout(()=>{
                this.loadTable();
            },100)
        },

        open(mypk) {
            this.$confirm('此操作将删除此用户, 是否继续?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
            }).then(() => {
                this.DeleteIt(mypk)
            })
        },

        // 删除
        DeleteIt(mypk) {
            var en = new Entity("BP.WF.Template.SelectAccper", mypk);
            en.Delete(); 
            setTimeout(()=>{
                this.loadTable();
            },100)
        },
        
        // select 
        changeSelect(inputKey){
            this.params.TB_Emps = inputKey;
            console.log(this.params);
            var handler = new this.HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            handler.AddJson(this.params); //参数ToNode, WorkID,FK_Node,FK_Flow
            var data = handler.DoMethodReturnString("AccepterOfGener_SelectEmps");
            console.log(data);
            this.options = JSON.parse(data);
        },

        //blurSelect
        blurSelect(){
            console.log(this.value);
            this.addEmpsExit(this.value)
        }

	},

	//监听
	computed: {},

	components: {},

	//监听后执行动作
	watch: {}
};
</script>

<style lang="less" scoped>
/deep/ .el-dialog__body {
	padding: 10px 20px;
}
/deep/ .el-button{
    margin: 0 5px;
}
</style>